
#include <vector>
#include <list>
#include <iomanip>
#include <iostream>  
#include <fstream> 
#include <tuple>
#include <utility>
#include <cmath>
#include <algorithm>
#include<functional>



#ifdef _win64系统
#include <windows.h>
#else
#include <ctime>
#endif

#include "../include/LMS.h"


using namespace std;

#define π 3.141592653589793238462643383279

#define 步长 100 //us
#define 周期 20 //ms
#define 周期数 100


const int 点数 = 周期 * 1000 * 周期数 / 步长;
floatx ww0[点数];
floatx ww1[点数];
floatx u[点数]; //合成信号
floatx s[点数]; //信号
int r[点数];    //reset信号
floatx y[点数];
floatx e[点数];
floatx miu[点数];


/* void 输出给文件( const floatx (&a)[] , const char * dir )
{
    //ofstream out( "../信号s.txt" , ios::out );
    ofstream out ,out2;
    out.open( "../给matlab/信号s.txt" , ios::out );
    for( auto i: s )    out << i <<endl;   
    out.close();
    out.open( "../给matlab/信号和噪声u.txt" , ios::out );
    for( auto i: u )    out << i <<endl;   
    out.close();
    out.open( "../给matlab/输出y.txt" , ios::out );
    for( auto i: y )    out << i <<endl;   
    out.close();
    out.open( "../给matlab/误差e.txt" , ios::out );
    for( auto i: e )    out << i <<endl;   
    out.close();

    out2.open( "../给matlab/权重w.txt" , ios::out );
    for( auto i=0 ; i<点数 ; i++ )    out2 << ww0[i]<<"   "<<ww1[i] <<endl;   
    out2.close();
}
 */

int main()
{   
    
    // floatx u[点数];//合成信号    
    // floatx s[点数];//信号
    // int r[点数];//reset信号
    // floatx y[点数];
    // floatx e[点数];
    // floatx miu[点数];
    
    void *p1 = LMS_Struct_Creat();
//    void *p2 = LMS_Struct_Creat();


    srand( (int)time(NULL) );  // 产生随机种子  把0换成NULL也行
    for (int i = 0; i < 点数; i++)
    {
        s[i] = -3 * cos( i*2*π/(周期*1000.0/(double)步长)  ) ;

        //int t = rand() % 100 ; //在100中产生随机数
        //float rt = 2.0*(float)(t-50)/100.0 ; // -1~1
        
        //u[i] = s[i] + 1*rt; 

        r[i] = (int)( i >0.0*点数 ) ;      
    }
    for (int i = 0; i < 点数 -2 ; i = i+3 ){
        int t = rand() % 100 ; //在100中产生随机数
        float rt = 2.0*(float)(t-50)/100.0 ; // -1~1
        u[i]   = s[i] + 1*rt;
        u[i+1] = s[i+1] + 1*rt;
        u[i+2] = s[i+2] + 1*rt;
    }




   

    // 计时
    #ifdef _win64系统
    LARGE_INTEGER t1,t2,tc;
    QueryPerformanceFrequency(&tc);
    QueryPerformanceCounter(&t1);
    #else
    clock_t time_start = clock();
#endif
    // 计时👆

    for (int i=0 ; i<点数; i ++){
        floatx yy,ee,miu1,ww[3];
        NLMS_Run(
            (LMS_Struct *)p1,
            r[i],
            50,
            u[i],
            u[i],
            &yy, &ee, &miu1
            );
        y[i] = yy;
        e[i] = ee;
        miu[i] = miu1;
        auto n = get_weight( (LMS_Struct *)p1 , &ww[0] ,3 );        
        ww0[i] = ww[0];
        ww1[i] = ww[1];

    }

    // 计时
    #ifdef _win64系统
    QueryPerformanceCounter(&t2);
    auto time=(double)(t2.QuadPart-t1.QuadPart)/(double)tc.QuadPart;     
    cout << "点数：" << 点数 <<endl;
    cout<<"time =  "<<1000.0*time<<"  ms"<<endl;  //输出时间（单位：ｓ）
    cout<<endl;
    cout<<endl;
    #else
    clock_t time_end = clock();
    cout << "点数：" << 点数 <<endl;
    cout << "time =  " << 1000.0 * (time_end - time_start) / (double)CLOCKS_PER_SEC << "  ms" << endl; //输出时间（单位：ｓ）
    cout << endl;
    cout << endl;    
#endif
    // 计时👆



    for(int i=0 ; i<点数*0.005; i ++){
        cout << "输入x：" << setw(5)<< left << u[i]
             << " 输入reset：" << setw(1)<< left << r[i]
             << " 输入s：" << setw(5)<< left << s[i]
             << "  输出y:" << setw(4)<< left << y[i] << "  输出e:" << setw(5)<< left << e[i] << "  系数μ:" << setw(7)<< left << miu[i]
             << " 系数w0："<< setw(6)<< left << ww0[i]<< setw(6)<< left << " 系数w1：" << ww1[i]
             << endl;
    }
    for(int i=点数*0.99 ; i<点数*1; i ++){
        cout << "输入x：" << setw(5)<< left << u[i]
             << " 输入reset：" << setw(1)<< left << r[i]
             << " 输入s：" << setw(5)<< left << s[i]
             << "  输出y:" << setw(4)<< left << y[i] << "  输出e:" << setw(5)<< left << e[i] << "  系数μ:" << setw(7)<< left << miu[i]
             << " 系数w0："<< setw(6)<< left << ww0[i]<< setw(6)<< left << " 系数w1：" << ww1[i]
             << endl;
    }
   


    //ofstream out( "../信号s.txt" , ios::out );
    ofstream out ,out2;
    out.open( "../给matlab/信号s.txt" , ios::out );
    for( auto i: s )    out << i <<endl;   
    out.close();
    out.open( "../给matlab/信号和噪声u.txt" , ios::out );
    for( auto i: u )    out << i <<endl;   
    out.close();
    out.open( "../给matlab/输出y.txt" , ios::out );
    for( auto i: y )    out << i <<endl;   
    out.close();
    out.open( "../给matlab/误差e.txt" , ios::out );
    for( auto i: e )    out << i <<endl;   
    out.close();

    out2.open( "../给matlab/权重w.txt" , ios::out );
    for( auto i=0 ; i<点数 ; i++ )    out2 << ww0[i]<<"   "<<ww1[i] <<endl;   
    out2.close();


    system("pause");

}
